package com.hj.util;

import android.util.Log;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            Type genType = o.getClass().getGenericSuperclass();
            if (!(genType instanceof ParameterizedType)) {
                Log.d("TUtil", "Does not find this type.");
                return null;
            }
            Log.d("TUtil", "Get this type success.");
            return ((Class<T>) ((ParameterizedType) genType).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
